package thread;

public class SyncDemo1 {
    public static void main(String[] args) {
        /*Shop s = new Shop();
        Thread t1 = new Thread("郭金磊"){
            public void run(){
                s.buy();
            }
        };

        Thread t2 = new Thread("王小明"){
            public void run(){
                s.buy();
            }
        };
        t1.start();
        t2.start();*/

        Shop s1 = new Shop();
        Shop s2 = new Shop();
        Thread t1 = new Thread("gjl"){
            public void run(){
                s1.buy();
            }
        };
        // Thread t1 = new Thread(s1::buy);
        Thread t2 = new Thread("chn"){
            public void run(){
                s2.buy();
            }
        };
        // Thread t2 = new Thread(s2::buy);
        t1.start();
        t2.start();
    }

}

class Shop{
    public void buy(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"正在买衣服");
            Thread.sleep(3000);
            //synchronized(new Object)
            //synchronized (123)
            //synchronized ("123")
            synchronized (this) {
                System.out.println(t.getName() + "正在试衣服");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+"买衣服完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
